package com.dshore.messagecenter.service.basedata.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dshore.messagecenter.dao.system.LoginDao;
import com.dshore.messagecenter.model.ExcelUp;
import com.dshore.messagecenter.model.MongoUser;
import com.dshore.messagecenter.model.system.User;
import com.dshore.messagecenter.service.basedata.LoginService;
import com.dshore.messagecenter.service.exception.PasswordNotMatchException;
import com.dshore.messagecenter.service.exception.UsernameNotFoundException;
import com.dshore.messagecenter.util.JsonUtil1;
import com.dshore.messagecenter.util.UUIDUtil;
@Service
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	private LoginDao loginDao;
	
	@Autowired
    private StringRedisTemplate redisTemplate;
	
	@Autowired
    private MongoOperations mongoTemplate;
	
	@Override
	public User login(String username, String password) {
		//根据用户名查找用户数据
		 User u=loginDao.findUserByUsername(username);
		 if(u!=null) {
			 if(u.getPassword().equals(password)) {
				 return u;
			 }else {
				 throw new PasswordNotMatchException("密码错误");
			 }
		 }else {
			 //u为null,用户名不存在
			 throw new UsernameNotFoundException("用户名不存在");
		 }
		 
	}

	@Override
	public Integer changePassword(String username,String password) {
		Date d=new Date();
		Timestamp ts = new Timestamp(d.getTime());
		Integer result=loginDao.changePassword(username,password);
		if(result==1) {
			return 0;
		}else {
			return -1;
		}
		
	}

	//操作redis
	@Override
	public Integer addredis(String username, String password, String tel) {
		User u=new User();
		u.setUsername(username);
		u.setPassword(password);
		u.setPhone(tel);
		System.out.println("u--------"+u);
		
		redisTemplate.opsForValue().set("user : "+UUIDUtil.getUUId(), JsonUtil1.object2json(u));
		return 1;
	}
	
	//查找redis
	@Override
	public List<User> lookredis(String id) {
		
	        User result =JSON.parseObject(redisTemplate.opsForValue().get("user : "+id),User.class);
	        if(result == null){
	           
	            redisTemplate.opsForValue().set("user : "+result.getId(), JsonUtil1.object2json(result));
	        }
	        List<User> u=new ArrayList();
	        u.add(result);
	        return u;
	}

	@Override
	public Integer addmongoDB(String username, String password, String tel) {
		MongoUser u=new MongoUser();
		u.setUid(UUID.randomUUID().toString());
		u.setUsername(username);
		u.setPassword(password);
		u.setTel(tel);
		mongoTemplate.save(u);
		return 1;
	}

	@Override
	public MongoUser lookmongoDB(String name) {
		Query query=new Query(Criteria.where("username").is(name));
		MongoUser user =  mongoTemplate.findOne(query , MongoUser.class);
        return user;
	}

	@Override
	public Integer insertExcel(List<ExcelUp> list) {
		Integer r=loginDao.insertExcel(list);
		return r;
	}

	@Override
	public List<ExcelUp> xzexcel() {
		List<ExcelUp> li=loginDao.xzexcel();
		return li;
	}

	@Override
	public Integer addIp(Map<String, Object> map) {
		Integer r=loginDao.addIp(map);
		return r;
	}
	
	
}
